<?php
#京东阿尔发
class erpapi_logistics_matrix_jdalpha_request_electron extends erpapi_logistics_request_electron
{
    public function bufferRequest($sdf){
        return $this->directNum;
    }
    public function directRequest($sdf){
        $this->primaryBn = $sdf['primary_bn'];

        $delivery     = $sdf['delivery'];
        $shopInfo     = $sdf['shop'];
        $from_address = $shopInfo['province']. $shopInfo['city'] .  $shopInfo['area'] . $shopInfo['address_detail'];
        $to_address   = $delivery['ship_addr'] ? $delivery['ship_province'] . $delivery['ship_city'] . $delivery['ship_district'] . $delivery['ship_addr'] : '_SYSTEM';

        $receiver = array(
            'contact'      => $delivery['ship_name'],
            'phone'        => $delivery['ship_tel']?$delivery['ship_tel']:$delivery['ship_mobile'],#座机没有，用手机
            'mobile'       => $delivery['ship_mobile'],
            'provinceName' => $delivery['ship_province'],
            'cityName'     => $delivery['ship_city'],
            'countryName'  => $delivery['ship_district'],
            'address'      => $this->charFilter($to_address),
        );

        $sender = array(
            'contact'      => $shopInfo['default_sender'],
            'phone'        => $shopInfo['tel']?$shopInfo['tel']:$shopInfo['mobile'],#座机没有，用手机
            'mobile'       => $shopInfo['mobile'],
            'provinceName' => $shopInfo['province'],
            'cityName'     => $shopInfo['city'],
            'countryName'  => $shopInfo['area'],
            'address'      => $this->charFilter( $from_address),
        );

        #加盟类型
        if($sdf['mode'] == 'join'){
            $params['branchCode'] = $sdf['jdalpha_businesscode'];#承运商发货网点编码
        }else{
        #直营类型
            $params['settlementCode'] = $sdf['jdalpha_businesscode'];#结算编码
        }

        $params['waybillType']      = '1';#1、普通运单;2、生鲜;3、航空。目前都不支持生鲜
        $params['waybillCount']     = '1';#所需运单的数量最多传99单
        $params['company_code']     =  $sdf['corp_type'];#承运商编码
        $params['salePlatform']     = $sdf['sale_plateform'];#0010001代表京东平台下的订单
        $params['platformOrderNo']  = $delivery['delivery_bn']. base_shopnode::node_id('ome');#平台订单号，即pop订单号，如果多订单合并发货，每个订单号之间用“，”逗号分隔,非京东平台,填所对应平台的订单号
        $params['vendorCode']       = $sdf['jdalpha_vendorCode'];#商家编码
        $params['vendorName']       = $shopInfo['shop_name'];#商家名称
        $params['VendorOrderCode']  = $delivery['delivery_bn'];#商家自有订单号
        $params['weight']           = number_format($delivery['weight'],2,".","");
        $params['volume']           = '0';#体积
        $params['promiseTimeType']  = 0;#承诺时效类型，无时效默认传0,ERP暂无时效
        $params['payType']          = '0';#付款方式0-在线支付 目前暂不支持货到付款业务
        $params['goodsMoney']       = number_format($delivery['total_amount'],2,".","");
        $params['shouldPayMoney']   = '0';#代收金额
        $params['needGuarantee']    = $sdf['is_protect']?$sdf['is_protect']:false;#是否要保价
        $params['guaranteeMoney']   = $sdf['protect_price']?number_format($sdf['protect_price'],2,".",""):0;#保价金额
        $params['receiveTimeType']  = '0';#收货时间类型，0任何时间，1工作日2节假日,ERP暂无收货时间
        $params['fromAddress']      = json_encode($sender);#京标发货四级地址
        $params['toAddress']        = json_encode( $receiver);#京标收货四级地址
        $params['expressPayMethod'] = $sdf['expressPayMethod'];#快递费付款方式
        $params['expressType']      = $sdf['expressType'];#快件产品类别
        $delivery['company_code']   = $sdf['corp_type'];

        $back =   $this->requestCall('store.trade.waybill.receive', $params,array(),$sdf);

        return $this->backToResult($back, $delivery);
    }
    public function backToResult($back, $delivery){
        $data = empty($back['data']) ? '' : json_decode($back['data'], true);
        if($back['rsp'] =='fail' || empty($data)) {
            $msg = $back['msg'] ? $back['msg'] : $back['err_msg'] ;
            return $msg;
        }
        $waybillCodeList = $data['msg']['waybillCodeList'];
        $result = array();
        $logi_no = $waybillCodeList[0];
        $waybillExtend['logistics_no'] =  $logi_no;
        $waybillExtend['delivery_bn']  =  $delivery['delivery_bn'];
        $waybillExtend['company_code'] =  $delivery['company_code'];
        $waybillExtendata = $this->waybillExtend($waybillExtend); #获取大头笔
        $result[] = array(
            'succ' => $waybillCodeList[0]? true : false,
            'msg' => '',
            'delivery_id' => $delivery['delivery_id'],
            'delivery_bn' => $delivery['delivery_bn'],
            'logi_no' => $logi_no,
            'position_no'        =>  $waybillExtendata['position'],#大头笔编码
            'position'           =>  $waybillExtendata['position_no'],#大头笔名称
            'package_wd'         =>  $waybillExtendata['package_wd'],#集包地编码
            'package_wdjc'       =>  $waybillExtendata['package_wdjc'],#集包地名称
        );
        $this->directDataProcess($result);
        return $result;
    }
    public function waybillExtend($sdf) {
        $this->title = '京东阿尔法获取大头笔';
        $this->primaryBn = $sdf['delivery_bn'];
        $params['logistics_no'] =  $sdf['logistics_no'];
        $params['company_code'] =  $sdf['company_code'];
        $result = $this->requestCall('store.trade.vendor.bigshot.query',$params);

        $data = $result['data'] ? json_decode($result['data'], true):'';
        $data = $data['msg'];
        if($result['rsp'] =='fail' || empty($data)) {
            return false;
        }
        $extend_data['position_no']         =  $data['bigShotCode'];#大头笔编码
        if($data['secondSectionCode']){
            $extend_data['position_no'] = $extend_data['position_no'].'-'.$data['secondSectionCode'];
        }
        if($data['thirdSectionCode']){
            $extend_data['position_no'] = $extend_data['position_no'].'-'.$data['thirdSectionCode'];
        }
        $extend_data['position']            =  $data['bigShotName'];#大头笔名称
        $extend_data['package_wd']          =  $data['gatherCenterCode'];#集包地编码
        $extend_data['package_wdjc']        =  $data['gatherCenterName'];#集包地名称
        return $extend_data;
    }

    public function recycleWaybill($waybillNumber) {
        $obj_delviery = app::get('ome')->model('delivery');
        $obj_orders   = app::get('ome')->model('orders');

        // 判断快递单是否补打
        $bill = app::get('ome')->model('delivery_bill')->dump(array('logi_no'=>$waybillNumber));
        if ($bill) {
            $delivery = $obj_delviery->dump(array('delivery_id'=>$bill['delivery_id']),'delivery_id,delivery_bn');

            $order_bns = array($delivery['delivery_bn'].'_'.$bill['log_id']);
        } else {
            $delivery = $obj_delviery->dump(array('logi_no'=>$waybillNumber),'delivery_id,delivery_bn');

            foreach ($obj_orders->getOrdersBnById($delivery['delivery_id']) as $value) {
                $order_bns[] = $value['order_bn'];
            }
        }

        app::get('logisticsmanager')->model('waybill')->update(array('status'=>2,'create_time'=>time()),array('waybill_number'=>$waybillNumber));

        $this->title = '京东阿尔法_' . $this->__channelObj->channel['logistics_code'] . '取消电子面单';

        $this->primaryBn = $waybillNumber;

        $opInfo = kernel::single('ome_func')->getDesktopUser();

        $params = array(
            'tid'          => implode(',', $order_bns),
            'company_code' => $this->__channelObj->channel['logistics_code'],
            'logistics_no' => $waybillNumber,
            'name'         => $opInfo['op_name']
        );
        $callback = array(
            'class' => get_class($this),
            'method' => 'callback'
        );
        $this->requestCall('store.trade.waybill.unbind', $params, $callback);
    }
}
